package org.jint.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	private static final String DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss";

	private static final String TIMEFORMAT = "yyyyMMddHHmmss";

	private static final String TIMEFORMATSSS = "yyyyMMddHHmmssSSS";

	private static final String DATEFORMAT = "yyyyMMdd";

	private static final String DATEFORMATyyy_MM_dd = "yyyy-MM-dd";

	public static String GetDateTime() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat(DATETIMEFORMAT);
		String mDateTime = formatter.format(cal.getTime());

		return (mDateTime);
	}

	public static String GetDate_yyyy_MM_dd() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat(DATEFORMATyyy_MM_dd);
		String mDateTime = formatter.format(cal.getTime());

		return (mDateTime);
	}

	public static String GetCurrentDate() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat(TIMEFORMAT);
		String mDateTime = formatter.format(cal.getTime());

		return (mDateTime);
	}

	public static String GetDateWithMSEL() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat(TIMEFORMATSSS);
		String mDateTime = formatter.format(cal.getTime());
		return (mDateTime);
	}

	public static String GetDate() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat(DATEFORMAT);
		String mDateTime = formatter.format(cal.getTime());
		return mDateTime;
	}

	/**
	 * 功能或作用：格式化日期时间
	 * 
	 * @param DateValue
	 *            输入日期或时间
	 * @param DateType
	 *            格式化 EEEE是星期, yyyy是年, MM是月, dd是日, HH是小时, mm是分钟, ss是秒
	 * @return 输出字符串
	 */
	public static String formatDate(String DateValue, String DateType) {
		String Result = "";
		SimpleDateFormat formatter = new SimpleDateFormat(DateType);
		try {
			java.util.Date mDateTime = formatter.parse(DateValue);
			Result = formatter.format(mDateTime);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (Result.equalsIgnoreCase("1900-01-01")) {
			Result = "";
		}
		return Result;
	}

	/**
	 * 功能或作用：格式化日期时间
	 * 
	 * @param DateValue
	 *            输入日期或时间
	 * @return 输出字符串
	 */
	public static String formatDate(String DateValue) {
		String Result = "";
		SimpleDateFormat formatter = new SimpleDateFormat(DATETIMEFORMAT);
		try {
			java.util.Date mDateTime = formatter.parse(DateValue);
			Result = formatter.format(mDateTime);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (Result.equalsIgnoreCase("1900-01-01")) {
			Result = "";
		}
		return Result;
	}

	public static java.util.Date getDate(String DateValue) {
		java.util.Date Result = null;
		SimpleDateFormat formatter = new SimpleDateFormat(DATETIMEFORMAT);
		try {
			Result = formatter.parse(DateValue);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return Result;
	}

	public static java.util.Date getDate_yyyy_MM_dd(String DateValue) {
		java.util.Date Result = null;
		SimpleDateFormat formatter = new SimpleDateFormat(DATEFORMATyyy_MM_dd);
		try {
			Result = formatter.parse(DateValue);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return Result;
	}

	/**
	 * 取得系统当前时间前n天
	 * 
	 * @param n
	 *            int
	 * @return String yyyymmdd
	 */
	public static String getNDayBeforeCurrentDate(int n) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -n);
		String math = null;
		String date = null;
		if (c.get(Calendar.MONTH) + 1 < 10) {
			math = "0" + (c.get(Calendar.MONTH) + 1);
		} else {
			math = String.valueOf(c.get(Calendar.MONTH) + 1);
		}
		if (c.get(Calendar.DATE) < 10) {
			date = "0" + c.get(Calendar.DATE);
		} else {
			date = String.valueOf(c.get(Calendar.DATE));
		}
		return "" + c.get(Calendar.YEAR) + math + date;
	}

	/**
	 * 取得系统当前时间前n个月的相对应的一天
	 * 
	 * @param n
	 *            int
	 * @return String yyyymmdd
	 */
	public static String getNMonthBeforeCurrentDay(int n) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -n);
		String math = null;
		String date = null;
		if (c.get(Calendar.MONTH) + 1 < 10) {
			math = "0" + (c.get(Calendar.MONTH) + 1);
		} else {
			math = String.valueOf(c.get(Calendar.MONTH) + 1);
		}
		if (c.get(Calendar.DATE) < 10) {
			date = "0" + c.get(Calendar.DATE);
		} else {
			date = String.valueOf(c.get(Calendar.DATE));
		}
		return "" + c.get(Calendar.YEAR) + math + date;

	}

	public static void main(String[] args) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, 2010);
		cal.set(Calendar.MONTH, 10);
		cal.set(Calendar.DAY_OF_MONTH, 10);
		
		cal.add(Calendar.DATE, 4);
		Date date = cal.getTime();
		System.out.println(df.format(date));
		
		cal.add(Calendar.DATE, -4);
		date = cal.getTime();
		System.out.println(df.format(date));
		
		cal.add(Calendar.DATE, -10);
		date = cal.getTime();
		System.out.println(df.format(date));
		
		cal.add(Calendar.DATE, -11);
		date = cal.getTime();
		System.out.println(df.format(date));
	}

}








